package trefliq.EJB.sessionbeans;

import java.util.List;
import javax.annotation.security.PermitAll;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import trefliq.EJB.entities.ShowBizPeople;

/**
 *
 * @author Eric
 */

@Stateless
@LocalBean
@PermitAll
public class ShowBizPeopleSession {
    
    @PersistenceContext(unitName="trefliqPU")
    private EntityManager em;

    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public List<ShowBizPeople> getAllShowBizPeople() {
        return em.createNamedQuery("findAllShowBizPeople", ShowBizPeople.class).getResultList();
    }
    
    public ShowBizPeople findShowBizPersonById(Long personId) {
        return em.createNamedQuery("findShowBizPersonById", ShowBizPeople.class).
                    setParameter("id", personId).getSingleResult();
    }
    
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public ShowBizPeople findShowBizPersonByLastName(String lname) {
        return em.createNamedQuery("findShowBizPersonByLastName", ShowBizPeople.class).
                    setParameter("lname", lname).getSingleResult();
    }
    
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public ShowBizPeople findShowBizPersonByFirstName(String fname) {
        return em.createNamedQuery("findShowBizPersonByFirstName", ShowBizPeople.class).
                    setParameter("fname", fname).getSingleResult();
    }
    
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public ShowBizPeople findShowBizPersonByName(String fname, String lname) {
        return em.createNamedQuery("findShowBizPersonByName", ShowBizPeople.class).
                    setParameter("lname", lname).setParameter("fname", fname).getSingleResult();
    }
    
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public ShowBizPeople createShowBizPerson(ShowBizPeople person) {

        em.persist(person);
        return person;
    }
    
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public ShowBizPeople deleteShowBizPerson(ShowBizPeople person) {
        
        em.remove(em.merge(person));
        return person;
    }
    
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public ShowBizPeople editShowBizPerson(ShowBizPeople person) {
        
        em.merge(person);
        return person;
    }
    
    // DB helper methods
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public void deleteAll() {
        Query q = em.createQuery("DELETE FROM ShowBizPeople p");
        q.executeUpdate();
    }
}
